﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;

//yuanzhen
namespace Jumpton.SFA.Server.BusinessRule
{
    
    public class MessageManager
    {
        /// <summary>
        /// 获取登录用户所有发件箱记录
        /// </summary>
        /// <returns></returns>
        public ObjectTable GetMessageViewList(string PERSONID, string DOMAINID, string updatetime)
        {
            return new ObjectTable(typeof(MessageView), string.Format("this.PERSONID={0} and this.DOMAINID={1} and this.BEGINDATE<='{2}' AND this.ENDDATE>='{3}'", PERSONID, DOMAINID, updatetime, updatetime));
        }


        /// <summary>
        /// 获取登录用户所有发件箱记录
        /// </summary>
        /// <returns></returns>
        public ObjectTable GetSendMessageList(decimal DOMAINID, string condition, string usercode)
        {
            condition += "  order by CREATE_TIME DESC ";
            return new ObjectTable(typeof(Message), string.Format("DOMAIN_ID={0} AND LAST_UPDATER='{1}' " + condition, DOMAINID, usercode));
        }
        /// <summary>
        /// 获取登录用户所有收件箱记录
        /// </summary>
        /// <returns></returns>
        public ObjectTable GetReceiveMessageList(decimal DOMAINID, string condition, string personid)
        {
            condition += " order by b.READ_FLAG ASC,a.CREATE_TIME DESC ";
            return new ObjectTable(typeof(MessageView), string.Format("a.DOMAIN_ID={0} AND b.PERSON_ID={1} "+condition, DOMAINID, personid));
        }

        /// <summary>
        /// 获取用户可用所有消息列表
        /// </summary>
        /// <returns></returns>
        public ObjectTable GetMessageList(string userid)
        {
            String condition = " order by b.READ_FLAG ASC,a.CREATE_TIME DESC ";
            return new ObjectTable(typeof(MessageView), string.Format("b.PERSON_ID in (Select person_id from users where id={0})and {1} between begin_date and end_date and a.ACTIVE='1' " + condition, userid, BusinessLogic.GetShortDateTimeString(DateTime.Now)));
        }


        public void UpdateReadCount(decimal messageid)
        {

            SqlData.ExecuteSql("update MESSAGE SET READ_COUNT=(SELECT COUNT(ID) FROM MESSAGE_RECEIVE WHERE MESSAGE_ID="+messageid+" AND READ_FLAG=1) WHERE ID="+messageid );
        }
        /// <summary>
        /// 获取登录用户所有发件箱记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetMessageReceiveList(string messageid)
        {
            return SqlData.OpenSP("sp_SelectMessageRecieveByMID", messageid).Tables[0];
        }   

        /// <summary>
        /// 获取单条记录值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message LoadMessage(decimal id)
        {
            
            return (Message)ObjectRow.Load(typeof(Message), string.Format("this.ID={0}", id));
        }
        /// <summary>
        /// 获取单条记录值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message LoadMessage(decimal DOMAINID,string updater,decimal LAST_UPDATE_TIME)
        {

            return (Message)ObjectRow.Load(typeof(Message), string.Format("this.DOMAINID={0} and LAST_UPDATER='{1}' and LAST_UPDATE_TIME={2}", DOMAINID, updater, LAST_UPDATE_TIME));
        }

        /// <summary>
        /// 获取单条记录值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message_Receive LoadMessageReceive(decimal id)
        {

            return (Message_Receive)ObjectRow.Load(typeof(Message_Receive), string.Format("this.ID={0}", id));
        }

        /// <summary>
        /// 获取单条记录值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message_Receive LoadMessageReceive(int messsageid,decimal userid)
        {
            return (Message_Receive)ObjectRow.Load(typeof(Message_Receive), string.Format("MESSAGE_ID={0} AND PERSON_ID={1}", messsageid, userid));
        }
        /// <summary>
        /// 保存更新消息
        /// </summary>
        /// <param name="message"></param>
        public void SaveMessage(Message message)
        {
            if (message != null)
            {
                
                message.Save();
            }
        }

        /// <summary>
        /// 保存更新消息接收信息
        /// </summary>
        /// <param name="messagereceive"></param>
        public void SaveMessage_Receive(Message_Receive messagereceive)
        {
            if (messagereceive != null)
            {
                //if (messagereceive.State != DataRowState.Added)
                //{
                //    UpdateReadCount(messagereceive.MESSAGEID); //更新已读消息数量，改成了触发器更新
                //}
                messagereceive.Save();
            }
        }


    }
}
